<?php

/**
 * @author HungPV <phamvanhung0818@gmail.com>
 * @todo managment projects controller
 */
class projects extends MX_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category');
        $this->load->model('project');
        $this->load->library('pagination');
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    function index($row = 0) {
        $this->user->check_permission();
        //paging
        include('paging.php');
        $config['per_page'] = 10;
        $config['base_url'] = base_url() . "/administrator/projects/index/";

        $lst_projects = $this->project->get(-1, $config['per_page'], $row);

        $config['total_rows'] = $this->project->get_count();
        $config['cur_page'] = $row;
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();
        //data tranfer        
        $data['lst_projects'] = $lst_projects;

        $data['view'] = 'projects_index';
        $this->load->view('template', $data);
    }

    function create() {
        $this->user->check_permission();
        if ($this->input->post('title')) {
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('hdffeatured_image');
            $cat_id = $this->input->post('projects_cat');
            $permalink = $this->project->generate_permalink($name);

            if ($this->project->add($name, $description, $content, $image, $cat_id, $permalink)) {
                $this->session->set_flashdata('insert_projects_success', true);
            } else {
                $this->session->set_flashdata('insert_projects_error', true);
            }
            redirect('administrator/projects/create');
        }

        // get list categories
        $categories = $this->category->get(-1,-1,0,'project');
        $data['categories'] = $categories;

        $data['view'] = 'projects_create';
        $this->load->view('template', $data);
    }

    function edit($id) {
        $this->user->check_permission();
        if ($this->input->post('title')) {
            $id = $this->input->post('id');
            $name = $this->input->post('title');
            $description = $this->input->post('description');
            $content = $this->input->post('content');
            $image = $this->input->post('hdffeatured_image');
            $cat_id = $this->input->post('projects_cat');            

            if ($this->project->edit($id,$name, $description, $content, $image, $cat_id)) {
                $this->session->set_flashdata('update_projects_success', true);
            } else {
                $this->session->set_flashdata('update_projects_error', true);
            }
            redirect('administrator/projects/edit/'.$id);
        }
        
        $projects = $this->project->get($id);
        $data['projects'] = $projects;
        // get list categories        
        $categories = $this->category->get(-1,-1,0,'project');
        $data['categories'] = $categories;
        
        
        $data['view'] = 'projects_edit';
        $this->load->view('template', $data);
    }

    function delete() {
        $this->user->check_permission();
        $id = $this->input->post('id');
        if ($this->project->delete($id)) {
            echo json_encode(array('result' => TRUE));
        } else {
            echo json_encode(array('result' => FALSE));
        }
    }

}

?>
